const getToken = () => {
  return uni.getStorageSync('token')
}

// 页面白名单 此白名单是需要登录才能访问的页面
const whiteList = ['/pages/user/index', '/pages/cart/cart1']

// 检查地址白名单
function checkWhite(url: string) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) === -1
}

// 页面跳转验证拦截器
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
list.forEach((item) => {
  uni.addInterceptor(item, {
    invoke(to) {
      console.log(checkWhite(to.url), to.url)
      if (checkWhite(to.url)) {
        return true
      } else {
        if (getToken()) {
          return true
        } else {
          uni.reLaunch({
            url: '/pages/login/login',
          })
          return false
        }
      }
    },
    fail(err) {
      console.log(err)
    },
  })
})
